home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / hips / sources / scale_geom / pic.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-17  |  3.9 KB  |  211 lines

  1. /* pic: device-independent picture package */
  2.  
  3. static char rcsid[] = "$Header: pic.c,v 2.1 88/11/01 21:09:56 ph Locked $";
  4.  
  5. #include "simple.h"
  6. #include "pic.h"
  7.  
  8. int pic_npic = -1;
  9. extern Pic pic_hips;
  10.  
  11. Pic *pic_list[PIC_LISTMAX] = { &pic_hips, 0};
  12.  
  13. void pic_init()
  14. {
  15.     /* count the pic device types to set npic */
  16.     for (pic_npic=0; pic_npic<PIC_LISTMAX && pic_list[pic_npic]; pic_npic++);
  17. }
  18.  
  19. Pic *pic_open(file, mode)
  20. char *file, *mode;
  21. {
  22.     return pic_open_dev("hips", file, mode);
  23. }
  24.  
  25. Pic *pic_open_dev(dev, name, mode)
  26. char *dev, *name, *mode;
  27. {
  28.     int i;
  29.     char *data;
  30.     Pic *p, *q;
  31.  
  32.     if (pic_npic<0) pic_init();
  33.     if (!dev) {            /* probably comes from pic_file_dev */
  34.     fprintf(stderr, "unknown pic device on %s\n", name);
  35.     return 0;
  36.     }
  37.     for (i=0; i<pic_npic && !str_eq(dev, pic_list[i]->dev); i++);
  38.     if (i>=pic_npic) {
  39.     fprintf(stderr, "unknown pic device: %s\n", dev);
  40.     return 0;
  41.     }
  42.     q = pic_list[i];
  43.     data = (*q->procs->open)(name, mode);
  44.     if (!data) return 0;
  45.  
  46.     /* copy the Pic structure before modifying it */
  47.     ALLOC(p, Pic, 1);
  48.     *p = *q;
  49.     p->data = data;
  50.     return p;
  51. }
  52.  
  53. void pic_close(p)
  54. Pic *p;
  55. {
  56.     (*p->procs->close)(p->data);
  57.     free(p);
  58. }
  59.  
  60. /* pic_catalog: print list of known (linked) device libraries */
  61.  
  62. void pic_catalog()
  63. {
  64.     int i;
  65.  
  66.     if (pic_npic<0) pic_init();
  67.     fprintf(stderr,"picture devices/file formats known:");
  68.     for (i=0; i<pic_npic; i++)
  69.     fprintf(stderr," %s", pic_list[i]->dev);
  70.     fprintf(stderr,"\n");
  71. }
  72.  
  73. Pic *pic_load(name1, name2)
  74. char *name1, *name2;
  75. {
  76.     Pic *p, *q;
  77.  
  78.     p = pic_open(name1, "r");
  79.     if (!p) {
  80.     fprintf(stderr, "pic_load: can't open %s\n", name1);
  81.     return 0;
  82.     }
  83.     q = pic_open(name2, "w");
  84.     if (!q) {
  85.     fprintf(stderr, "pic_load: can't open %s\n", name2);
  86.     pic_close(p);
  87.     return 0;
  88.     }
  89.     pic_copy(p, q);
  90.     pic_close(p);
  91.     return q;
  92. }
  93.  
  94. void pic_save(p, name)
  95. Pic *p;
  96. char *name;
  97. {
  98.     Pic *q;
  99.  
  100.     q = pic_open(name, "w");
  101.     if (!q) {
  102.     fprintf(stderr, "pic_save: can't create %s\n", name);
  103.     return;
  104.     }
  105.     pic_copy(p, q);
  106.     pic_close(q);
  107. }
  108.  
  109. void pic_copy(p, q)
  110. register Pic *p, *q;
  111. {
  112.     int nchan, dx, y;
  113.     Window w;
  114.     Pixel1 *buf;
  115.     Pixel1_rgba *buf4;
  116.  
  117.     nchan = pic_get_nchan(p);
  118.     pic_set_nchan(q, nchan);
  119.     pic_set_window(q, pic_get_window(p, &w));
  120.     dx = w.x1-w.x0+1;
  121.     switch (nchan) {
  122.     case 1:
  123.         ALLOC(buf, Pixel1, dx);
  124.         break;
  125.     case 3:
  126.     case 4:
  127.         ALLOC(buf4, Pixel1_rgba, dx);
  128.         break;
  129.     default:
  130.         fprintf(stderr, "pic_copy: can't handle nchan=%d\n", nchan);
  131.         return;
  132.     }
  133.     for (y=w.y0; y<=w.y1; y++)
  134.     switch (nchan) {
  135.         case 1:
  136.         pic_read_row(p, y, w.x0, dx, buf);
  137.         pic_write_row(q, y, w.x0, dx, buf);
  138.         break;
  139.         case 3:
  140.         case 4:
  141.         pic_read_row_rgba(p, y, w.x0, dx, buf4);
  142.         pic_write_row_rgba(q, y, w.x0, dx, buf4);
  143.         break;
  144.     }
  145.     if (nchan==1) free(buf); else free(buf4);
  146. }
  147.  
  148. void pic_set_window(p, win)
  149. Pic *p;
  150. Window *win;
  151. {
  152.     pic_set_box(p, win->x0, win->y0, win->x1-win->x0+1, win->y1-win->y0+1);
  153. }
  154.  
  155. void pic_write_block(p, x0, y0, nx, ny, buf)
  156. Pic *p;
  157. int x0, y0, nx, ny;
  158. Pixel1 *buf;
  159. {
  160.     int y;
  161.  
  162.     for (y=0; y<ny; y++, buf+=nx)
  163.     pic_write_row(p, y0+y, x0, nx, buf);
  164. }
  165.  
  166. void pic_write_block_rgba(p, x0, y0, nx, ny, buf)
  167. Pic *p;
  168. int x0, y0, nx, ny;
  169. Pixel1_rgba *buf;
  170. {
  171.     int y;
  172.  
  173.     for (y=0; y<ny; y++, buf+=nx)
  174.     pic_write_row_rgba(p, y0+y, x0, nx, buf);
  175. }
  176.  
  177. Window *pic_get_window(p, win)
  178. Pic *p;
  179. Window *win;
  180. {
  181.     int dx, dy;
  182.  
  183.     if (!win) ALLOC(win, Window, 1);
  184.     pic_get_box(p, &win->x0, &win->y0, &dx, &dy);
  185.     win->x1 = win->x0+dx-1;
  186.     win->y1 = win->y0+dy-1;
  187.     return win;
  188. }
  189.  
  190. void pic_read_block(p, x0, y0, nx, ny, buf)
  191. Pic *p;
  192. int x0, y0, nx, ny;
  193. Pixel1 *buf;
  194. {
  195.     int y;
  196.  
  197.     for (y=0; y<ny; y++, buf+=nx)
  198.     pic_read_row(p, y0+y, x0, nx, buf);
  199. }
  200.  
  201. void pic_read_block_rgba(p, x0, y0, nx, ny, buf)
  202. Pic *p;
  203. int x0, y0, nx, ny;
  204. Pixel1_rgba *buf;
  205. {
  206.     int y;
  207.  
  208.     for (y=0; y<ny; y++, buf+=nx)
  209.     pic_read_row_rgba(p, y0+y, x0, nx, buf);
  210. }
  211.